#!/usr/bin/python
#Shreads contigs file into fragments of desired sizes. 
#Example: use these reads as 'fake' reads in 454 assemblies.
#Usage: shredOverlappingReads.py seq.fasta 400 100
#Note: overlap shouldn't be > 50% of contig length

import sys
import re
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord

seqfile = sys.argv[1]
sf = SeqIO.parse(seqfile, "fasta")

size = int(sys.argv[2])
overlap = int(sys.argv[3])

start = 0
stop = size

for s in sf:
    start = 0
    stop = size
    while stop <= len(s.seq)+size:
        sid = s.id + "_" + str(start) + "_" + str(start + len(s.seq[start:stop]))
        rec = s.seq[start:stop]
        record = SeqRecord(rec, id=sid, description="")
        print record.format("fasta").strip("\n")
        if stop > len(s.seq):
            break #stop if last one is already printed
        start += size - overlap
        stop += size - overlap

